package com.thejavacode.quartz;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringQuartzTrigger {
	
	private Scheduler scheduler = null;
	
	public SpringQuartzTrigger(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	public Scheduler getScheduler() {
		return scheduler;
	}

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	public void task() throws SchedulerException, InterruptedException
    {
		if(scheduler != null){
			System.out.println("Scheduler iniciado. Comenzando tareas...");
			scheduler.start();
	        Thread.sleep(10000);
	        scheduler.shutdown();
		}
    }
    
    public static void main (String args[]) 
    {
    	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-ctx.xml");
        Scheduler scheduler = (Scheduler) ctx.getBean("scheduler");
        try {
        	SpringQuartzTrigger runner = new SpringQuartzTrigger(scheduler);
        	runner.task();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
